package com.briup.estore.web.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.briup.estore.dto.OrderCreateDTO;
import com.briup.estore.dto.OrderPayDTO;
import com.briup.estore.dto.OrderPaySuccessDTO;
import com.briup.estore.dto.OrderSearchDTO;
import com.briup.estore.service.OrderService;
import com.briup.estore.vo.OrderInfoVO;
import com.briup.estore.web.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping()
    @ApiOperation("创建订单")
    public Result createOrder(@RequestBody OrderCreateDTO orderCreateDTO, HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        orderService.createOrder(orderCreateDTO, Integer.parseInt(userId));
        return Result.success();
    }
//
    @GetMapping("/")
    @ApiOperation("获取用户的订单列表")
    public Result getOrders(HttpServletRequest request, OrderSearchDTO orderSearchDTO) {
        String userId = (String) request.getAttribute("userId");
        List<OrderInfoVO> vos = orderService.getOrders(Integer.parseInt(userId), orderSearchDTO);
        return Result.success(vos);
    }

//    @GetMapping("/")
//    @ApiOperation("订单详情")
//    public Result ordlist(HttpServletRequest request){
//        String userID = (String) request.getAttribute("UserID");
//        List<OrderInfoVO> orderlist = orderService.ordlist(userID);
//        return Result.success(orderlist);
//    }
    @Autowired
    private AlipayClient alipayClient;

    @PutMapping("/pay")
    @ApiOperation("支付订单")
    public Result toPayOrder(@RequestBody OrderPayDTO order) { //订单编号，订单金额，回调首页8080
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        //订单支付
        model.setBody("商品描述：");
        //订单名字
        model.setSubject("杰普书城购物订单");
        //订单号
        model.setOutTradeNo(order.getOrderId());
        //过期时间
        model.setTimeoutExpress("30m");
        //订单金额
        model.setTotalAmount(order.getTotalPrice());
        //产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        //设置参数
        alipayTradePagePayRequest.setBizModel(model);
        //回调地址
        alipayTradePagePayRequest.setReturnUrl(order.getPathUrl());
        //客户端执行,拿到支付结果
        AlipayTradePagePayResponse pageExecute = null;
        try {
            pageExecute = alipayClient.pageExecute(alipayTradePagePayRequest);

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        String result = pageExecute.getBody();
        return Result.success(result);
    }

    //该方法支付成功后自动调用
    @ApiOperation("支付成功后修改订单状态")
    @PutMapping("/paySuccess")
    public Result paySuccess(@RequestBody OrderPaySuccessDTO paySuccessDTO) {
        orderService.paySuccess(paySuccessDTO);
        return Result.success();
    }
}

